package algoU2A1;

import java.awt.image.BufferedImage;
import java.awt.image.PixelGrabber;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class U2A1_V2 {

	int[] m_Pix;
	int[][] m_Histogram;

	public static void main(String[] args) {
		try {
			new U2A1_V2(ImageIO.read(new File("C:/pony.png")));
		} catch (IOException e) {
			System.out.println("ImageFile not Found!");
		}
	}

	U2A1_V2(BufferedImage img) {
		m_Pix = new int[img.getWidth() * img.getHeight()];
		PixelGrabber p = new PixelGrabber(img, 0, 0, img.getWidth(),
				img.getHeight(), m_Pix, 0, img.getWidth());
		try {
			p.grabPixels();
		} catch (InterruptedException e) {
		}
		new Histogram(m_Pix).print();
	}
}

class Histogram {
	int[][] histogram;
	int length = 1, writePosition = 1, increasePosition = 0;
	int[] values;

	Histogram(int[] data) {
		values = data;
		java.util.Arrays.sort(values);
		for (int i = 0; i < values.length - 1; ++i) {
			if (values[i] != values[i + 1])
				++length;
		}

		histogram = new int[length][2];

		histogram[0][1] = values[0];
		histogram[0][0] = 1;

		for (int i = 1; i < values.length - 1; ++i) {
			if (values[i] != values[i - 1]) {
				histogram[writePosition][1] = values[i];
				++writePosition;
			}
		}

		for (int i = 0; i < values.length - 1; ++i) {
			if (values[i + 1] != values[i]) {
				++increasePosition;
			}
			++histogram[increasePosition][0];
		}
	}

	public void print() {
		for (int i = 0; i < histogram.length; ++i) {
			System.out.print(histogram[i][0] + "|" + histogram[i][1]);
			System.out.println();
		}
	}
}